<?php

namespace app\api\model;

use app\common\common;
use app\common\model\MemberDonation as MemberDonationModel;
use think\facade\Db;
use app\common\model\UserMoneyLog as UserMoneyLog;
class MemberDonation extends MemberDonationModel
{
    /**
     * 添加关于我们
     */
    public static function add($data){
        Db::startTrans();
        try {

            $data['add_time'] = date('Y-m-d H:i:s',time());
            $data['update_time'] = date('Y-m-d H:i:s',time());
            $data['images'] = html_entity_decode($data['images']);

            $id = self::insertGetId($data);
            //判断是否有捐赠
            if($data['money'] > 0){
                $currency = common::key_currency($data['type_money']);  //读取币种
                $log = UserMoneyLog::change($data['mid'],$currency,-$data['money'],'joinus',$id,'用户慈善捐赠：'.$data['money']);
                if($log != true){  //慈善捐赠
                    Db::rollback();
                    return false;
                }
            }

            // 提交事务
            Db::commit();
            return $id;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return false;
        }
    }


    /**
     * 统计慈善活动捐赠
     * @param $cid 活动ID
     * @param $type_money   币种
     * @return float    统计金额
     */
    public static function statistics($cid = 1,$type_money = 1){
        $money = self::where('cid',$cid)->where('type_money',$type_money)->sum('money');
        return $money;
    }
}